package com.googlecode.c4e.ui.wizards;

import java.net.URL;

import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class AddServerWizardPage extends WizardPage {
	private Text urlText;
	private Text labelText;
	private Text usernameText;
	private Text passwordText;

	/**
	 * Constructor for SampleNewWizardPage.
	 * 
	 * @param pageName
	 */
	public AddServerWizardPage(ISelection selection) {
		super("wizardPage");
		setTitle("Adding new Continuum Server");
		setDescription("This wizard allows user to add new Continuum Server to list of managed servers.");
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		layout.verticalSpacing = 9;

		GridData gd = new GridData(GridData.FILL_HORIZONTAL);

		// Label
		Label labelLabel = new Label(container, SWT.NULL);
		labelLabel.setText("Label");

		labelText = new Text(container, SWT.BORDER | SWT.SINGLE);
		labelText.setLayoutData(gd);
		labelText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});

		// URL
		Label urlLabel = new Label(container, SWT.NULL);
		urlLabel.setText("URL");

		urlText = new Text(container, SWT.BORDER | SWT.SINGLE);
		urlText.setLayoutData(gd);
		urlText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});

		Label usernameLabel = new Label(container, SWT.NULL);
		usernameLabel.setText("Username");

		usernameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		usernameText.setLayoutData(gd);
		usernameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});

		Label passwordLabel = new Label(container, SWT.NULL);
		passwordLabel.setText("Password");

		passwordText = new Text(container, SWT.BORDER | SWT.SINGLE
				| SWT.PASSWORD);
		passwordText.setLayoutData(gd);
		passwordText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});

		// initialize();
		dialogChanged();
		setControl(container);
	}

	/**
	 * Ensures that both text fields are set.
	 */

	private void dialogChanged() {
		String fileName = getUrl();

		if (fileName.trim().length() == 0) {
			updateStatus("URL must be specified");
			return;
		}
		try {
			new URL(fileName);
		} catch (Exception e) {
			updateStatus("URL is not valid");
			return;
		}

		if (getLabel().trim().length() == 0) {
			updateStatus("Label must be specified");
		}
		updateStatus(null);
	}

	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}

	public String getLabel() {
		return labelText.getText();
	}

	public String getUrl() {
		return urlText.getText();
	}

	public String getUsername() {
		return usernameText.getText();
	}

	public String getPassword() {
		return passwordText.getText();
	}

}